package com.stillalive.blogsystem.controller;

import com.stillalive.blogsystem.pojo.dataobject.BlogInfo;
import com.stillalive.blogsystem.pojo.request.AddBlogRequest;
import com.stillalive.blogsystem.pojo.request.UpdateBlogRequest;
import com.stillalive.blogsystem.pojo.response.BlogInfoResponse;
import com.stillalive.blogsystem.service.BlogService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Resource(name = "blogServiceImpl")
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfoResponse> getList(){
        log.info("获取博客列表...");
        List<BlogInfoResponse> blogInfos = blogService.getList();
        return blogInfos;
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfoResponse getBlogDetail(@NotNull Integer blogId){
        log.info("获取博客详情，blogId: {}", blogId);
        return blogService.getBlogDetail(blogId);
    }

    @RequestMapping("/addBlog")
    public Boolean addBlog(@RequestBody @Validated AddBlogRequest addBlogRequest){
        log.info("添加博客，userId: {}, title: {}", addBlogRequest.getUserId(), addBlogRequest.getTitle());
        return blogService.addBlog(addBlogRequest);
    }

    @RequestMapping("/updateBlog")
    public Boolean updateBlog(@RequestBody @Validated UpdateBlogRequest updateBlogRequest){
        log.info("更新博客，blogId: {}", updateBlogRequest.getId());
        return blogService.updateBlog(updateBlogRequest);
    }

    @RequestMapping("/deleteBlog")
    public Boolean deleteBlog(@NotNull(message = "id不能为null..")Integer blogId){
        log.info("删除博客，blogId: {}", blogId);
        return blogService.deleteBlog(blogId);
    }
}
